package top.haijunit.work.modules.core;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.haijunit.work.core.doamin.*;

import java.util.Collection;

/**
 * 【说明类功能】
 *
 * @author zhanghaijun
 * @since 2024-08-27
 */
public abstract class BaseController<S extends BaseService<M, T, D>, M extends BaseMapper<T>, T extends BaseEntity, D> {

    @Autowired
    S baseService;

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public ResultPage<D> listPage(D dto, PageQuery pageQuery) {
        return baseService.selectPageList(dto, pageQuery);
    }

    @Operation(summary = "列表查询")
    @GetMapping("/list")
    public Result<Collection<D>> list(D dto) {
        return Result.success(baseService.selectList(dto));
    }

    @Operation(summary = "详情查询")
    @GetMapping("/detail/{code}")
    public Result<D> detail(@PathVariable("code") Long code) {
        return Result.success(baseService.selectDetail(code));
    }

    @Operation(summary = "新增")
    @PostMapping("/add")
    public Result<Long> add(@RequestBody @Validated(ValidChecks.GroupAdd.class) D dto) {
        Long code = baseService.insert(dto);
        return Result.success(code);
    }

    @Operation(summary = "修改")
    @PutMapping("/edit")
    public Result<Long> update(@RequestBody @Validated(ValidChecks.GroupEdit.class) D dto) {
        Long code = baseService.modify(dto);
        return Result.success(code);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{code}")
    public Result<Long> delete(@PathVariable Long code) {
        return Result.success(baseService.delete(code));
    }

}
